package com.lane.sidecn.product.model;

import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.StringUtils;
import org.compass.annotations.SearchableComponent;
import org.compass.annotations.SearchableId;
import org.compass.annotations.SearchableProperty;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.lane.core.dao.extend.UndeleteableEntityOperation;

/**
 * 产品.带jpa与hibernate扩展的annotation 及compass annotation.
 *
 * @author cac
 * @author schweigen
 */

@Entity
@Table(name="PRODUCT")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("product")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Product {
	private static final int SHORT_DESCN_LENGTH = 200;

	@SearchableId
	private Integer id;

	/**
	 * 类别
	 */
	@SearchableComponent
	private Category category;

	/**
	 * 库存量
	 */
	private Integer inventory;

	/**
	 * 产品名称
	 */
	@SearchableProperty
	private String name;

	/**
	 * 产品完整描述
	 */
	private String descn;

	/**
	 * 单价
	 */
	private Double unitprice=0.0;

	/**
	 * 状态
	 */
	private String status = UndeleteableEntityOperation.NORMAL_VALUE;

	/**
	 * 简要描述
	 */
	private String shortDescn = null;

	@ManyToOne
	@JoinColumn(name = "CATEGORY_ID")
	public Category getCategory() {
		return this.category;
	}

	public String getDescn() {
		return this.descn;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Integer getId() {
		return id;
	}

	public Integer getInventory() {
		return this.inventory;
	}

	public String getName() {
		return this.name;
	}

	@Transient
	public String getShortDescn() {
		if (shortDescn == null) {
			shortDescn = StringUtils.substring(descn, 0, SHORT_DESCN_LENGTH) + "...";
		}
		return shortDescn;
	}

	public String getStatus() {
		return this.status;
	}

	public Double getUnitprice() {
		return this.unitprice;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public void setDescn(String descn) {
		this.descn = descn;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public void setInventory(Integer inventory) {
		this.inventory = inventory;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setShortDescn(String shortDescn) {
		this.shortDescn = shortDescn;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public void setUnitprice(Double unitprice) {
		this.unitprice = unitprice;
	}
}